package com.benbi.shequapi.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.benbi.shequapi.bean.*;
import com.benbi.shequapi.mapper.*;
import com.benbi.shequapi.service.ReportService;
import com.benbi.shequapi.utils.Result;
import com.benbi.shequapi.utils.ResultData;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class ReportImpl implements ReportService {
    @Resource
    ReportMapper reportMapper;
    @Resource
    PersonMapper personMapper;


    @Override
    public Result getReportList() {

        List<Report> resultList;
        resultList = reportMapper.selectReportList();
        if (resultList == null) {return new Result(1, "不存在报告");}
        return new ResultData(resultList);
    }

    @Override
    public Result getReportByRepid(int rep_id) {
        Report p = reportMapper.selectReportByRepid(rep_id);
        if (p == null) {return new Result(1, "不存在该任务");}
        JSONObject o = new JSONObject();
        o.putAll((JSONObject) JSONObject.toJSON(p));
        System.out.println(p.getW_pid());
        o.put("name", personMapper.selectPersonNameById(p.getW_pid()));
//        jsonObject.putAll((JSONObject) JSONObject.toJSON(person));
        return new ResultData(o);
    }

    @Override
    public Result newReport(int w_pid,String title,String content) {
        Date d = new Date();
        Report r = new Report(0,w_pid,title,content,d);
        int p = reportMapper.insertReport(r);
        return new Result(0, "成功");
    }

    @Override
        public Result deleteReport(int rep_id){
        int p = reportMapper.deleteReportByRepid(rep_id);
        return new Result(0, "成功");
    }



}
